package GUI;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.Font;


@SuppressWarnings("serial")
/**
 * Register a new specific user to the library
 * @author Benny
 *
 */
public class RegisterNewUser extends JPanel {
	public JTextField textName;
	public JTextField textId;
	public JTextField textEmail;
	public JTextField textPhone;
	public JTextField textUserID;
	public JTextField textPassword;
	public MainFrame mf;
	
	/**
	 * initialize all the text fields
	 */
	public void init(){
		textName.setText("");
		textId.setText("");
		textEmail.setText("");
		textPhone.setText("");
		textUserID.setText("");
		textPassword.setText("");
	}
	
	/**
	 * check for email validity
	 * @return validity status
	 */
	public int checkEmail(){
		String email = textEmail.getText();
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();

		if(matchFound) return 1;
		else return 0;
	}
	/**
	 * check for phone validity
	 * @return validity status
	 */
	public int checkPhone(){
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
        CharSequence inputStr = textPhone.getText();
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches();

		if(matchFound) return 1;
		else return 0;
    }
	/**
	 * check for userID validity
	 * @return user's ID validity status
	 */
	public int checkID(){
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{3})$";
        CharSequence inputStr = textId.getText();
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches();

		if(matchFound) return 1;
		else return 0;
    }
	/**
	 * check for user's Name validity
	 * @return user's Name validity status
	 */
	public int checkName()
	{
	  final char[] chars = textName.getText().toCharArray();
	  for (int x = 0; x < chars.length; x++) {      
	    final char c = chars[x];
	    if ((c >= 'a') && (c <= 'z')) continue; // lowercase
	    if ((c >= 'A') && (c <= 'Z')) continue; // uppercase
	    return 0;
	  }  
	  return 1;
	}
	

	/**
	 * Create the panel.
	 * @param M Instance of the main Frame of the program 
	 */
	public RegisterNewUser(MainFrame M) {
		setPreferredSize(new Dimension(600, 520));
		setMaximumSize(new Dimension(600, 520));
		setMinimumSize(new Dimension(600, 520));
		setLayout(null);
		mf=M;
		JLabel lblRegistrationSystem = new JLabel("Registration System");
		lblRegistrationSystem.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblRegistrationSystem.setBounds(219, 12, 189, 23);
		add(lblRegistrationSystem);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(162, 92, 96, 14);
		add(lblName);
		
		JLabel lblId = new JLabel("Id:");
		lblId.setBounds(162, 117, 96, 14);
		add(lblId);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(162, 142, 84, 14);
		add(lblEmail);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(162, 170, 46, 14);
		add(lblPhone);
		
		textName = new JTextField();
		textName.setBounds(295, 89, 140, 20);
		add(textName);
		textName.setColumns(10);
		
		textId = new JTextField();
		textId.setBounds(295, 114, 140, 20);
		add(textId);
		textId.setColumns(10);
		
		textEmail = new JTextField();
		textEmail.setBounds(295, 139, 140, 20);
		add(textEmail);
		textEmail.setColumns(10);
		
		textPhone = new JTextField();
		textPhone.setBounds(295, 167, 140, 20);
		add(textPhone);
		textPhone.setColumns(10);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if((checkEmail()==1) && (checkPhone()==1) && (checkID()==1) && (checkName()==1))
				{
					ArrayList<Object> send = new ArrayList<>();
					int userID=Integer.decode(textId.getText());
					send.add("2");
					send.add("AddNewUser");
					send.add(textName.getText());
					send.add(userID);
					send.add(textEmail.getText());
					send.add(textPhone.getText());
					mf.client.accept(send);
				}
				else JOptionPane.showMessageDialog(null, "Incorrect User Details", "Program", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnRegister.setBounds(162, 211, 89, 23);
		add(btnRegister);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(335, 211, 89, 23);
		add(btnClear);
		
		textUserID = new JTextField();
		textUserID.setEditable(false);
		textUserID.setBounds(295, 286, 140, 20);
		add(textUserID);
		textUserID.setColumns(10);
		
		JLabel lblUserid = new JLabel("UserID:");
		lblUserid.setBounds(162, 289, 84, 14);
		add(lblUserid);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(162, 314, 84, 14);
		add(lblPassword);
		
		textPassword = new JTextField();
		textPassword.setEditable(false);
		textPassword.setBounds(295, 311, 140, 20);
		add(textPassword);
		textPassword.setColumns(10);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(683, 575);
		add(picLabel);

	}

}
